package com.esisa.IHM;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.esisa.beans.Etudiant;

public class ListerTousLesDocuments extends JFrame  {
	
	JTable table;
	JTextField t1;
	JButton b1;
	ModelDynamiqueDocument model;

	public ListerTousLesDocuments(final String choix) {
		
		setExtendedState(getExtendedState() | this.MAXIMIZED_BOTH );
		model=new ModelDynamiqueDocument();
		setLayout(new BorderLayout());
		JPanel p1=new JPanel();
		JLabel l1=new JLabel("chercher :");
		t1=new JTextField(20);
		t1.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent arg0) {
			}
			public void keyReleased(KeyEvent e) {
					if(e.getKeyChar()==8){
						model.init();
						model.autorecherche(t1.getText());	
						
					}
					model.autorecherche(t1.getText());	
				
			}
			public void keyPressed(KeyEvent arg0) {
			}
		});/*
		b1=new JButton("chercher");
		b1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String champRecherche=t1.getText();
				model.rechercherexact(champRecherche);
			}
		});
		*/
		p1.add(l1);p1.add(t1);//p1.add(b1);
	
		table=new JTable(model);
		table.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent arg0) {
			}
			public void mousePressed(MouseEvent arg0) {
			}
			public void mouseExited(MouseEvent arg0) {
			}
			public void mouseEntered(MouseEvent arg0) {
			}
			public void mouseClicked(MouseEvent e) {
				if (choix.equals("sup")) {
					int rownumb=table.getSelectionModel().getMaxSelectionIndex();
					if(e.getClickCount()==2){
						int rep=JOptionPane.showConfirmDialog(null,new String("Etes vous sur de supprimer cet Etudiant du master THYP ?"),"supprimer",JOptionPane.YES_NO_OPTION);
						if (rep==JOptionPane.YES_OPTION) {
							model.removeGeneralDocument(rownumb);
						}
					}
					
				}
				if (choix.equals("modi")) {
					int rownumb=table.getSelectionModel().getMaxSelectionIndex();
					if(e.getClickCount()==2){
						Etudiant d=new Etudiant(
								String.valueOf(model.getValueAt(rownumb, 0)),
								String.valueOf(model.getValueAt(rownumb, 1)),
								String.valueOf(model.getValueAt(rownumb, 2)),
								String.valueOf(model.getValueAt(rownumb, 3)),
								String.valueOf(model.getValueAt(rownumb, 4)),
								String.valueOf(model.getValueAt(rownumb, 5)),
								String.valueOf(model.getValueAt(rownumb, 6)),
								String.valueOf(model.getValueAt(rownumb, 7))
								
						);
						new  InterfaceModifierDocument(d);
						dispose();
					}
					
				}
			}
		});
		JScrollPane p=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		p1.setLayout(new FlowLayout(FlowLayout.LEFT));
		add(p1,BorderLayout.NORTH);
		add(p,BorderLayout.CENTER);
		setVisible(true);
	}

}
